/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.mapping;

import org.telosys.persistence.TelosysPersistenceException;
import org.telosys.util.StrUtil;

public class LinkMapping {

	private final Class<?>  type ; 
	
	private final String    name ;
    
    private final char      cardinality ;

//    private final String[]  keyValues ;

    private final String[]  keyParts ;

	//public LinkMapping(Class<?> type, String name, char cardinality, String keyValuesList, String keyPartsList ) {
	public LinkMapping(Class<?> type, String name, char cardinality, String keyPartsList ) {
		super();
		//--- Check args validity
		if ( null == type ) throw new IllegalArgumentException("Type is null");
		if ( null == name ) throw new IllegalArgumentException("Name is null");
		if ( cardinality != '1' && cardinality != 'N' ) throw new IllegalArgumentException("Invalid cardinality");
		if ( null == keyPartsList ) throw new IllegalArgumentException("keyValuesList is null");
		//if ( null == keyPartsList && cardinality != '1' ) throw new IllegalArgumentException("keyPartsList is null (required in 'To Many' link)");
		
		this.type = type;
		this.name = name;
		this.cardinality = cardinality;
		//this.keyValues = getStringsArrays(keyValuesList, "keyValues") ;
		this.keyParts  = getStringsArrays(keyPartsList,  "keyParts") ;
	}
    
	
	public Class<?> getType() {
		return type;
	}


	public String getName() {
		return name;
	}


	public char getCardinality() {
		return cardinality;
	}


//	public String[] getKeyValues() {
//		return keyValues;
//	}

	public String[] getKeyParts() {
		return keyParts;
	}

	public boolean isManyToOne() {
		return ( cardinality == '1' ) ;
	}


	private String[] getStringsArrays ( String list, String type ) {
		if ( list != null ) {
			String[] elements = StrUtil.split(list, ',') ;
			String[] keyParts = new String[elements.length] ;
			for ( int i = 0 ; i < elements.length ; i++ ) {
				String s = elements[i];
				if ( null == s ) {
					throw new TelosysPersistenceException("Invalid " + type + " in Link mapping (null)");
				}
				String t = s.trim();
				if ( t.length() == 0 ) {
					throw new TelosysPersistenceException("Invalid " + type + " in Link mapping (void)");
				}
				keyParts[i] = t;
			}
			return keyParts ;
		}
		else {
			return null ;
		}
	}
}
